#include "uiobjectreader.h"
#include <QFile>
#include <QtXml>
#include <QDomElement>
#include <QDebug>

UiObjectReader::UiObjectReader(QObject *parent)
    : QObject(parent)
{
}

QStringList UiObjectReader::readObjectNames(const QString &filePath)
{
    QStringList result;
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Cannot Open UI File" << filePath;
        return result;
    }

    QDomDocument doc;
    if (!doc.setContent(&file)) {
        qWarning() << "Unable To Parse UI File:" << filePath;
        file.close();
        return result;
    }
    file.close();

    // 查找所有 widget/action 元素的 name 属性
    QDomNodeList widgets = doc.elementsByTagName("widget");
    for (int i = 0; i < widgets.count(); i++) {
        QDomElement elem = widgets.at(i).toElement();
        if (elem.hasAttribute("name")) {
            result << elem.attribute("name");
        }
    }

    QDomNodeList actions = doc.elementsByTagName("action");
    for (int i = 0; i < actions.count(); i++) {
        QDomElement elem = actions.at(i).toElement();
        if (elem.hasAttribute("name")) {
            result << elem.attribute("name");
        }
    }

    return result;
}
